package com.vgooo.client;

import java.util.Stack;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.vgooo.util.DebugTools;
import com.vgooo.util.Utils;

/**
 * 用于记录打开的Activity，解决按返回键混乱的问题
 * @author Administrator
 *
 */
public class ViewLayout {
	
	public final static String className = "ViewLayout" ;
	
	
	//用于存储打开过的页面的，解决按返回键、后退按钮出现的混乱情况
	public  static Stack<ViewLayout> viwStack =null;
	
	
	public final static String TAB_TAG_HOME = "home";
	public final static String TAB_TAG_MOBILE = "moblie";
	public final static String TAB_TAG_VCHANNEL = "vchannel";
	public final static String TAB_TAG_MYVG = "myvg";
	public final static String TAB_TAG_TOOL = "tool";

	public final static String TAB_TAG_SEARCH = "search";
	public final static String TAB_TAG_ADV_DETAIL = "adv_detail";
	public final static String TAB_TAG_MOBILE_DETAIL = "moblie_detail";
	public final static String TAB_TAG_MOBILE_ALBUM = "moblie_album";
	public final static String TAB_TAG_LOGIN = "login";
	public final static String TAB_TAG_MYLOGIN = "mylogin";
	public final static String TAB_TAG_PHOTO = "photo";
	public final static String TAB_TAG_BRANCHS_MAIN = "branchs_main";
	public final static String TAB_TAG_BRANCHS_MAP = "branchs_map";
	public final static String TAB_TAG_TEST_MAIN = "test_main";
	public final static String TAB_TAG_ALBUM_DETAIL = "album_detail";
	public final static String TAB_TAG_VEDEIO_DETAIL = "vedio_detail";
	public final static String TAB_TAG_NEWS_DETAIL = "news_detail";
	public final static String TAB_TAG_IMAGEZOOM = "imageZoom";
	public final static String TAB_TAG_REGISTER = "register";
	public final static String TAB_TAG_PING = "ping";
	

	
	
//	public final static String TAB_TAG_TEST_AUDIO = "audio";
//	public final static String TAB_TAG_TEST_BUTTON = "button";
//	public final static String TAB_TAG_TEST_CAMERA = "camera";
//	public final static String TAB_TAG_TEST_COMMUN = "commun";
//	public final static String TAB_TAG_TEST_DISPLAY = "display";
//	public final static String TAB_TAG_TEST_HST = "hst";
//	public final static String TAB_TAG_TEST_LED = "led";
//	public final static String TAB_TAG_TEST_LIGHT= "light";
//	public final static String TAB_TAG_TEST_SHAKE = "shake";
//	public final static String TAB_TAG_TEST_TOUCH = "touch";
	
	
	public final static int ID_TAG_HOME = R.id.home_select_1back;
	public final static int ID_TAG_MOBILE = R.id.home_select_2mobile;
	public final static int ID_TAG_VCHANNEL = R.id.home_select_3vchannel;
	public final static int ID_TAG_MYVG = R.id.home_select_4myvg;
	public final static int ID_TAG_TOOL = R.id.home_select_5tool;
	public final static int ID_TAG_SEARCH = R.id.index_head_search;
	
	
	private static String currentTag = "";
	
	
	public boolean showTitle;
	public String title;
	public int checkedId ;
	public String tabTag ;
	public Activity activity ;
	
	
	/**
	 * 构造函数
	 * @param showTitle boolean 显示标题 
	 * 						值 ：
	 * 						true 显示标题、返回按钮，隐藏示LOGO， 必须传入标题（title）
	 * 						false 隐藏标题、返回按钮，显示LOGO， 标题（title）可为null
	 * @param title  String 显示的标题
	 * @param tabTag int 显示Activity的标记 值Main.TAB_TAG_MOBILE_DETAIL
	 * @param checkedId  导航栏的ID（R.id.home_select_5tool） 仅用于底部导航和顶部的返回、搜索、工具
	 */
	public ViewLayout(boolean showTitle, String title, int checkedId,
			String tabTag) {
		super();
		this.showTitle = showTitle;
		this.title = title;
		this.checkedId = checkedId;
		this.tabTag = tabTag;
	}
	
	
	/**
	 * 构造函数
	 * @param showTitle boolean 显示标题 
	 * 						值 ：
	 * 						true 显示标题、返回按钮，隐藏示LOGO， 必须传入标题（title）
	 * 						false 隐藏标题、返回按钮，显示LOGO， 标题（title）可为null
	 * @param title  String 显示的标题
	 * @param tabTag int 显示Activity的标记 值Main.TAB_TAG_MOBILE_DETAIL
	 * @param checkedId  导航栏的ID（R.id.home_select_5tool） 仅用于底部导航和顶部的返回、搜索、工具
	 * @param activity 将要启动的Activity
	 */
	
	
	public ViewLayout(boolean showTitle, String title, int checkedId,
			String tabTag, Activity activity) {
		super();
		this.showTitle = showTitle;
		this.title = title;
		this.checkedId = checkedId;
		this.tabTag = tabTag;
		this.activity = activity;
	}





	/**
	 * 
	 * @param showTitle boolean 显示标题 
	 * 						值 ：
	 * 						true 显示标题、返回按钮，隐藏示LOGO， 必须传入标题（title）
	 * 						false 隐藏标题、返回按钮，显示LOGO， 标题（title）可为null
	 * @param title  String 显示的标题
	 * @param tabTag int 显示Activity的标记 值Main.TAB_TAG_MOBILE_DETAIL
	 * @param checkedId  导航栏的ID（R.id.home_select_5tool） 
	 * 					仅用于底部导航和顶部的返回、搜索、工具 有固定的ID
	 * 					其他页面的ID采用自增 
	 * 					ID用来区分不同页面和进入及出去的动画
	 */
	 
	public static void setView(boolean showTitle, String title,String tabTag,int checkedId , boolean isPre ,Activity activity){
		
		if(isPre&&((checkedId == Main.currentTabId)||(currentTag.equals(tabTag)))){
//			if(!tabTag.equals(TAB_TAG_LOGIN)){
				return ;
//			}
		}
		
		if(isPre){
			ViewLayout.viwStack.push(new ViewLayout(showTitle, title, checkedId, tabTag,activity));
		}
		currentTag = tabTag ;
		final boolean o;
		// 设置显示内容出去的动画
		if (Main.currentTabId < checkedId) {
			o = true;
			Main.tabHost.getCurrentView().startAnimation(Main.left_out);
		} else {
			o = false;
			Main.tabHost.getCurrentView().startAnimation(Main.right_out);
		}
		//控制标题和返回按钮的显示
		if(showTitle){
			Main.title.setVisibility(View.VISIBLE);
			Main.title.setText(title);
//			Main.title2.setText(title);
			Main.back.setVisibility(View.VISIBLE);
			Main.logo.setVisibility(View.GONE);
		}else{
			Main.title.setVisibility(View.GONE);
//			Main.title.setText(title);当标题隐藏时可不更改
			Main.back.setVisibility(View.GONE);
			Main.logo.setVisibility(View.VISIBLE);
		}
		//将页面标志改为当前页面的标志
		Main.tabHost.setCurrentTabByTag(tabTag);//Main.TAB_TAG_MOBILE_DETAIL);
		//根据也页面标记（tabTag）来控制导航栏的显示与隐藏
		if(tabTag.equals(TAB_TAG_ADV_DETAIL)
				||tabTag.equals(TAB_TAG_MOBILE_DETAIL)
				||tabTag.equals(TAB_TAG_ALBUM_DETAIL)
				||tabTag.equals(TAB_TAG_NEWS_DETAIL)
				||tabTag.equals(TAB_TAG_MOBILE_ALBUM)
				||tabTag.equals(TAB_TAG_BRANCHS_MAP)
				||tabTag.equals(TAB_TAG_PHOTO)
				||tabTag.equals(TAB_TAG_LOGIN)
				||tabTag.equals(TAB_TAG_TOOL)
				||tabTag.equals(TAB_TAG_SEARCH)
				||tabTag.equals(TAB_TAG_IMAGEZOOM)
				||tabTag.equals(TAB_TAG_VEDEIO_DETAIL)
//				||tabTag.equals(TAB_TAG_BRANCHS_MAIN)
//				||tabTag.equals(TAB_TAG_TEST_AUDIO)
//				||tabTag.equals(TAB_TAG_TEST_BUTTON)
//				||tabTag.equals(TAB_TAG_TEST_CAMERA)
//				||tabTag.equals(TAB_TAG_TEST_COMMUN)
//				||tabTag.equals(TAB_TAG_TEST_DISPLAY)
//				||tabTag.equals(TAB_TAG_TEST_HST)
//				||tabTag.equals(TAB_TAG_TEST_LED)
//				||tabTag.equals(TAB_TAG_TEST_LIGHT)
//				||tabTag.equals(TAB_TAG_TEST_SHAKE)
//				||tabTag.equals(TAB_TAG_TEST_TOUCH)
				){
			Main.navLayout.setVisibility(View.GONE);
		}else{
			Main.navLayout.setVisibility(View.VISIBLE);
		}
		//设置显示页面进入的动画
		if (o) {
			Main.tabHost.getCurrentView().startAnimation(Main.left_in);
		} else {
			Main.tabHost.getCurrentView().startAnimation(Main.right_in);
		}
		//将页面的ID改为当前页面的ID
		Main.currentTabId = checkedId;
		//把即将要显示的视图信息压入栈顶
		
		
		//设置导航栏的背景颜色
		Main.setNavBackground(checkedId);
		
	}
	
	/**
	 * 
	 * @param showTitle boolean 显示标题 
	 * 						值 ：
	 * 						true 显示标题、返回按钮，隐藏示LOGO， 必须传入标题（title）
	 * 						false 隐藏标题、返回按钮，显示LOGO， 标题（title）可为null
	 * @param title  String 显示的标题
	 * @param tabTag int 显示Activity的标记 值Main.TAB_TAG_MOBILE_DETAIL
	 */
	public static void setView(boolean showTitle,String title,String tabTag,boolean isPre,Activity activity){
		setView(showTitle,title, tabTag, Main.currentTabId+1, isPre,activity);
	}
	
	/**
	 * 
	 * @param showTitle boolean 显示标题 
	 * 						值 ：
	 * 						true 显示标题、返回按钮，隐藏示LOGO， 必须传入标题（title）
	 * 						false 隐藏标题、返回按钮，显示LOGO， 标题（title）可为null
	 * @param title  String 显示的标题
	 * @param tabTag int 显示Activity的标记 值Main.TAB_TAG_MOBILE_DETAIL
	 */
	public static void setViewPre(boolean showTitle, String title,String tabTag,int checkedId,Activity activity){
		setView(showTitle, title, tabTag, checkedId , true,activity);
	}
	public static void setViewPre(boolean showTitle, String title,String tabTag,Activity activity ){
//		if(TAB_TAG_MYVG.equals(tabTag)&&ViewLayout.viwStack.peek().tabTag.equals(TAB_TAG_LOGIN)){
//			return ;
//		}
		setView(showTitle, title, tabTag,true,activity);
	}
	
	/**
	 * 单击返回按钮时
	 * 当前的View移除
	 * 显示上一个View并将
	 */
//	public static void setViewBack(Activity activity){
//		DebugTools.log(className, viwStack.toString());
//		if(ViewLayout.viwStack==null||ViewLayout.viwStack.isEmpty()||ViewLayout.viwStack.size()==1){
////			activity.finish();
//		}else{
//			ViewLayout.viwStack.pop();
//			ViewLayout view = ViewLayout.viwStack.peek();
//			setView(view.showTitle, view.title, view.tabTag,view.checkedId , false);
////			activity.finish();
//			Log.i(className, "setViewBack(): " + view +"\n     ----   " +  viwStack);
//		}
//	}
	
	/**
	 * 单击返回按钮时
	 * 当前的View移除
	 * 显示上一个View并将
	 */
	public static void setViewBack(){
		DebugTools.log(className, viwStack.toString());
		if(ViewLayout.viwStack!=null&&ViewLayout.viwStack.isEmpty()&&ViewLayout.viwStack.size()==1){
//			activity.finish();
//			ViewLayout.viwStack.pop().activity.finish();
		}else if(ViewLayout.viwStack!=null&&ViewLayout.viwStack.isEmpty()){
		}else{
			if(ViewLayout.viwStack!=null&&ViewLayout.viwStack.size()>1){
				ViewLayout view1 = ViewLayout.viwStack.pop();
				ViewLayout view = ViewLayout.viwStack.peek();
				if(view1.tabTag.equals(TAB_TAG_LOGIN)&&view.tabTag.equals(TAB_TAG_MYVG)&&(null==VGApplication.userInfo||!VGApplication.userInfo.isLogin)){
					view1 = ViewLayout.viwStack.pop();
					view = ViewLayout.viwStack.peek();
//					Log.i(className,"***********************   view : " + view);
				}
				if(view1.tabTag.equals(TAB_TAG_MYLOGIN)&&view.tabTag.equals(TAB_TAG_MYVG)&&(null==VGApplication.userInfo||!VGApplication.userInfo.isLogin)){
					view1 = ViewLayout.viwStack.pop();
					view = ViewLayout.viwStack.peek();
//					Log.i(className,"***********************   view : " + view);
				}
				setView(view.showTitle, view.title, view.tabTag,view.checkedId , false,null);
//				Log.i(className, "setViewBack():   isLogin : "  +VGApplication.userInfo.isLogin +   "    view1:" + view1 +"\n    view  : "+view+"\n ----   " +  viwStack);
			}
		}
	}

	public static void showCurrentView(){
		ViewLayout currentViewLayout = viwStack.peek();
		setView(currentViewLayout.showTitle	, currentViewLayout.title, currentViewLayout.tabTag, Main.currentTabId ++, false,null);
	}

	@Override
	public String toString() {
		return "ViewLayout [showTitle=" + showTitle + ", title=" + title
				+ ", checkedId=" + checkedId + ", tabTag=" + tabTag
//				+ ", activity=" + activity 
				+"]";
	}

}
